package com.pp.ent.db;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.pp.ent.util.GenericsUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by dengqu on 2017/2/24.
 */

public class BaseDao<T, ID> {

    private <D extends Dao<T, ID>> D getDao() {
        Class<T> genricCls = GenericsUtils.getSuperClassGenricType(getClass());
        try {
            return DbManager.getDbContext().getDbHelper().getDao(genricCls);
        } catch (SQLException e) {
        }
        return null;
    }

    public DeleteBuilder<T, ID> getDeleteBuiler()
    {
        return getDao().deleteBuilder();
    }

    public UpdateBuilder<T, ID> getUpdateBuiler()
    {
        return getDao().updateBuilder();
    }

    public QueryBuilder<T, ID> getQueryBuiler()
    {
        return getDao().queryBuilder();
    }

    public void insert(final T t, String cb)
    {
        DbCommand<Dao.CreateOrUpdateStatus> cmd = new DbCommand(cb)
        {
            public Dao.CreateOrUpdateStatus execute()
                    throws Exception
            {
                return BaseDao.this.getDao().createOrUpdate(t);
            }
        };
        DbManager.getDbContext().sendCommand(cmd);
    }

    public void delete(final T t, String cb)
    {
        DbCommand<Integer> cmd = new DbCommand(cb)
        {
            public Integer execute()
                    throws Exception
            {
                return Integer.valueOf(BaseDao.this.getDao().delete(t));
            }
        };
        DbManager.getDbContext().sendCommand(cmd);
    }

    public void deleteById(final ID id, String cb)
    {
        DbCommand<Integer> cmd = new DbCommand(cb)
        {
            public Integer execute()
                    throws Exception
            {
                return Integer.valueOf(BaseDao.this.getDao().deleteById(id));
            }
        };
        DbManager.getDbContext().sendCommand(cmd);
    }

    public void delete(final DeleteBuilder builder, String cb)
    {
        DbCommand<Integer> cmd = new DbCommand(cb)
        {
            public Integer execute()
                    throws Exception
            {
                return Integer.valueOf(builder.delete());
            }
        };
        DbManager.getDbContext().sendCommand(cmd);
    }

    public void update(final T t, String cb)
    {
        DbCommand<Integer> cmd = new DbCommand(cb)
        {
            public Integer execute()
                    throws Exception
            {
                return Integer.valueOf(BaseDao.this.getDao().update(t));
            }
        };
        DbManager.getDbContext().sendCommand(cmd);
    }

    public void update(final UpdateBuilder builder, String cb)
    {
        DbCommand<Integer> cmd = new DbCommand(cb)
        {
            public Integer execute()
                    throws Exception
            {
                return Integer.valueOf(builder.update());
            }
        };
        DbManager.getDbContext().sendCommand(cmd);
    }

    public void query(final ID id, String cb)
    {
        DbCommand<T> cmd = new DbCommand(cb)
        {
            public T execute()
                    throws Exception
            {
                return (T)BaseDao.this.getDao().queryForId(id);
            }
        };
        DbManager.getDbContext().sendCommand(cmd);
    }

    public void query(final QueryBuilder<T, ID> builder, String cb)
    {
        DbCommand<List<T>> cmd = new DbCommand(cb)
        {
            public List<T> execute()
                    throws Exception
            {
                return builder.query();
            }
        };
        DbManager.getDbContext().sendCommand(cmd);
    }
}